package com.xiaoxu.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoxu.base.exception.bkSchoolException;
import com.xiaoxu.content.dao.TeachplanMapper;
import com.xiaoxu.content.dao.TeachplanMediaMapper;
import com.xiaoxu.content.model.dto.BindTeachplanMediaDto;
import com.xiaoxu.content.model.po.Teachplan;
import com.xiaoxu.content.model.po.TeachplanMedia;
import com.xiaoxu.content.service.TeachplanMediaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaoxu
 */
@Slf4j
@Service
public class TeachplanMediaServiceImpl extends ServiceImpl<TeachplanMediaMapper, TeachplanMedia> implements TeachplanMediaService {

    private final TeachplanMediaMapper teachplanMediaMapper;

    private final TeachplanMapper teachplanMapper;

    public TeachplanMediaServiceImpl(TeachplanMediaMapper teachplanMediaMapper,TeachplanMapper teachplanMapper) {
        this.teachplanMediaMapper = teachplanMediaMapper;
        this.teachplanMapper = teachplanMapper;
    }

    /**
     * @description 教学计划绑定媒资
     * @param bindTeachplanMediaDto 教学计划媒资绑定DTO
     */
    @Override
    @Transactional
    public void associationMedia(BindTeachplanMediaDto bindTeachplanMediaDto) {
        //教学计划id
        Long teachplanId = bindTeachplanMediaDto.getTeachplanId();
        Teachplan teachplan = teachplanMapper.selectById(teachplanId);
        if(teachplan==null){
            throw new bkSchoolException("教学计划不存在");
        }
        Integer grade = teachplan.getGrade();
        if(grade!=2){
            throw new bkSchoolException("只允许第二级教学计划绑定媒资文件");
        }
        //课程id
        Long courseId = teachplan.getCourseId();
        //先删除原来该教学计划绑定的媒资
        teachplanMediaMapper.delete(new LambdaQueryWrapper<TeachplanMedia>().eq(TeachplanMedia::getTeachplanId, teachplanId));
        //再添加教学计划与媒资的绑定关系
        TeachplanMedia teachplanMedia = new TeachplanMedia();
        teachplanMedia.setCourseId(courseId);
        teachplanMedia.setTeachplanId(teachplanId);
        teachplanMedia.setMediaFilename(bindTeachplanMediaDto.getFileName());
        teachplanMedia.setMediaId(bindTeachplanMediaDto.getMediaId());
        teachplanMedia.setCreateDate(LocalDateTime.now());
        int insertCount = teachplanMediaMapper.insert(teachplanMedia);
        if (insertCount<= 0){
            throw new bkSchoolException("错误，插入失败，请联系管理人员");
        }
    }

    /**
     * @description 教学计划绑定媒资删除
     * @param teachplanId 教学计划Id
     * @param teachplanMediaId 绑定媒资Id
     */
    @Override
    @Transactional
    public void deleteAssociation(Long teachplanId, Long teachplanMediaId) {
        TeachplanMedia teachplanMedia = teachplanMediaMapper.selectById(teachplanMediaId);
        if (teachplanMedia == null) {
            throw new bkSchoolException("错误，当前没有绑定该媒资，不能删除");
        }
        if (!teachplanId.equals(teachplanMedia.getTeachplanId())){
            throw new bkSchoolException("错误，当前绑定关系错误，请联系管理员恢复数据");
        }
        LambdaQueryWrapper<TeachplanMedia> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TeachplanMedia::getTeachplanId,teachplanId);
        queryWrapper.eq(TeachplanMedia::getId,teachplanMediaId);
        int count = teachplanMediaMapper.delete(queryWrapper);
        if (count<=0){
            throw new bkSchoolException("错误，删除失败，请联系管理人员");
        }
    }
}
